
package com.faner.fast.security.component;

import com.faner.fast.constant.enums.CodeEnum;
import com.faner.fast.security.exception.FastAuth2Exception;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.SneakyThrows;

/**
 * OAuth2 异常格式化
 * @作者 faner
 * @创建时间 2021/1/15 18:01
 */
public class FastAuth2ExceptionSerializer extends StdSerializer<FastAuth2Exception> {

    public FastAuth2ExceptionSerializer() {
        super(FastAuth2Exception.class);
    }

    @Override
    @SneakyThrows
    public void serialize(FastAuth2Exception value, JsonGenerator gen, SerializerProvider provider) {
        gen.writeStartObject();
        gen.writeObjectField("code", CodeEnum.FAIL.getCode());
        gen.writeStringField("msg", value.getMessage());
        gen.writeStringField("data", value.getErrorCode());
        gen.writeEndObject();
    }

}
